﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace LdCms.Web.Controllers.API.Extend.V1
{
    using LdCms.EF.DbModels;
    using LdCms.Common.Extension;
    using LdCms.Common.Utility;
    using LdCms.IBLL.Extend;
    using LdCms.Web.Services;

    /// <summary>
    /// 
    /// </summary>
    [ApiVersion("1.0")]
    [ControllerName("extend/search/keyword")]
    public class ExtendSearchKeywordController : BaseApiController
    {
        private readonly IBaseApiManager BaseApiManager;
        private readonly IHttpContextAccessor Accessor;
        private readonly ISearchKeywordService SearchKeywordService;
        public ExtendSearchKeywordController(IBaseApiManager BaseApiManager, IHttpContextAccessor Accessor, ISearchKeywordService SearchKeywordService) : base(BaseApiManager)
        {
            this.BaseApiManager = BaseApiManager;
            this.Accessor = Accessor;
            this.SearchKeywordService = SearchKeywordService;
        }

        [HttpGet]
        [ActionName("top")]
        public IActionResult GetArticeTop(string uuid)
        {
            long logId = 0;
            try
            {
                logId = BaseApiManager.SaveLogs(uuid);
                string companyId = InterfaceAccount.CompanyID;
                string memberId = GetQueryString("memberid");
                int count = Utility.ToTopTotal(GetQueryString("count").ToInt());

                List<Ld_Extend_SearchKeyword> lists = new List<Ld_Extend_SearchKeyword>();
                if (string.IsNullOrWhiteSpace(memberId))
                    lists = SearchKeywordService.GetSearchKeywordByTop(SystemID, companyId, memberId, count);
                else
                    lists = SearchKeywordService.GetSearchKeywordByMemberId(SystemID, companyId, memberId, count);
                if (lists == null)
                    return Error("not data!");
                var data = from m in lists
                           select new
                           {
                               keyword = m.Keyword.IIF(string.Empty),
                               hits = m.Hits.ToInt()
                           };
                int total = data.Count();
                return Success(logId, "ok", new { total, rows = data });
            }
            catch (Exception ex)
            {
                return Error(logId, ex.Message);
            }
        }


    }
}